<template>
  <div class="basicForm">
    <BasicForm @register="registerForm">
      <!-- 插槽（备注信息） -->
      <template #remarks="{ model, field }">
        <WangEditor
          v-model:value="model[field]"
          :bizKey="record.id"
          :bizType="'testDataChild_' + field"
          :height="300"
        />
      </template>
    </BasicForm>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderMainSaleSingleForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicTable, useTable } from '/@/components/Table';
  import { WecharCusInvPriceRoute } from '/@/api/setting/price/wecharCusInvPriceRoute';

  import { BasicUpload } from '/@/components/Upload';
  import { WangEditor } from '/@/components/WangEditor';
  import { formSchemas } from '/@/utils/custom';

  const props = defineProps({
    schemas: { type: Array, default: [] },
    data: { type: Object, default: {} },
    formConfig: { type: Object, default: {} },
  });
  let inputFormSchemas: FormSchema[] = [];
  watch(
    () => props,
    () => {
      inputFormSchemas = formSchemas(
        props.schemas,
        props.formConfig.formColNum,
        props.data,
      ) as FormSchema[];

      inputFormSchemas = inputFormSchemas.map((item) => {
        if (item.field == 'chdefine15') {
          item.componentProps.params = {
            ccuscode: props.data.hfmdCode ? props.data.hfmdCode : props.data.cusCode,
          };
          item.componentProps.checkbox = false;
          item.componentProps.onSelect = async (e) => {
            let obj = await getFieldsValue();
            setFieldsValue({
              ...obj,
              chdefine18: e[0].cdeliverunit,
              chdefine22: e[0].cenglishadd4,
            });
          };
        }
        return item;
      });

      setTimeout(() => {
        zdy(props.data);
      }, 0);
    },
    { immediate: true },
  );

  const { t } = useI18n('test.testData');
  const record = ref<WecharCusInvPriceRoute>({} as WecharCusInvPriceRoute);
  const [registerForm, { resetFields, setFieldsValue, validate, setProps, getFieldsValue }] =
    useForm({
      compact: true,
    });
  async function zdy(data) {
    data.reader = `${data.bread ? data.bread : ''}  /  ${data.reader ? data.reader : ''}  /  ${
      data.readTime ? data.readTime : ''
    }`;
    data.rejectReason = `${data.rejectReason ? data.rejectReason : ''}  /  ${
      data.readMsg ? data.readMsg : ''
    }`;
    data.badvorderEdit = `${data.badvorder ? data.badvorder : ''}  /  ${
      data.badvorderEdit ? data.badvorderEdit : ''
    }`;
    record.value = data;
    // inputFormSchemas.forEach((item) => {
    //   if (item.field === 'cusCode') {
    //     item.dynamicDisabled = !record.value.isNewRecord;
    //   }
    //   if (item.field === 'invCode') {
    //     item.dynamicDisabled = !record.value.isNewRecord;
    //   }
    // });
    setProps({
      labelWidth: 120, // 控件前面的标签宽度
      schemas: inputFormSchemas, // 控件定义列表
      baseColProps: { lg: parseInt(props.formConfig.formColNum), md: 24 },
    });
    await resetFields(); // 重置表单数据
    await setFieldsValue(data); // 设置字段值
  }

  async function validateForm() {
    try {
      const data = await validate(); // 验证表单，并返回数据
      return data;
    } catch (error: any) {
      if (error && error.errorFields) {
        return false;
      }
    }
  }
  defineExpose({
    validateForm,
  });
</script>
<style>
  .col {
    color: red;
    font-weight: 100;
  }
</style>
